﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WXY.Course.Model;
using WXY.Course.Web.Areas.Admin.Models;

namespace WXY.Course.Web.Areas.Admin.Filter
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class DefaultAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            UserInfo userInfo = null;
            if ((userInfo = AdminContext.GetCurrentUser()) == null)
            {
                string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsoluteUri;
                string redirectUrl = string.Format("{0}?ReturnUrl={1}", FormsAuthentication.LoginUrl, HttpUtility.UrlEncode(redirectOnSuccess));

                filterContext.Result = new RedirectResult(redirectUrl);
                return;
            }

            HttpContext.Current.Items["UserInfo"] = userInfo;
        }
    }
}